<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <title>购物车</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <style>
    @import url("cart.css");
  </style>
</head>

<body>
  <h1>我的购物车</h1>

  <!-- 主容器：左 70% 右 30% -->
  <div class="main-box">
    <!-- 左边：购物车 -->
    <div class="cart-left" id="cart-left"></div>

    <!-- 右边：更多推荐（竖排 4 本书） -->
    <div class="rec-right">
      <h2>更多推荐</h2>
      <div class="rec-card">
        <img src="C:\Users\dante\Pictures\Screenshots\new1.webp" alt="">
        <div class="rec-info">
          <h6>了不起的盖茨比</h6>
          <p>￥32.8</p>
        </div>
      </div>
      <div class="rec-card">
        <img src="C:\Users\dante\Pictures\Screenshots\new2.webp" alt="">
        <div class="rec-info">
          <h6>红岩</h6>
          <p>￥59.0</p>
        </div>
      </div>
      <div class="rec-card">
        <img src="C:\Users\dante\Pictures\Screenshots\new3.webp" alt="">
        <div class="rec-info">
          <h6>茶花女</h6>
          <p>￥35.0</p>
        </div>
      </div>
      <div class="rec-card">
        <img src="C:\Users\dante\Pictures\Screenshots\new4.webp" alt="">
        <div class="rec-info">
          <h6>骆驼祥子</h6>
          <p>￥55.0</p>
        </div>
      </div>
    </div>
  </div>

  <!-- 底部：去购买 -->
  <div class="bottom-btn">
    <button class="buy-btn" onclick="location.href='pay.html'">去购买</button>
  </div>

  <!-- 只读本地 + 渲染 -->
  <script>
    function render() {
      const list = JSON.parse(localStorage.getItem('myCart')) || [];
      const box = document.getElementById('cart-left');
      if (!list.length) {
        box.innerHTML = '<div class="empty">购物车是空的<br><a href="index.html">去逛逛</a></div>';
        return;
      }
      box.innerHTML = list.map(item => `
        <div class="card">
          <img src="${item.img}" alt="book">
          <div class="info">
            <h3>${item.name}</h3>
            <p>单价：￥${item.price.toFixed(2)} | 数量：${item.qty}</p>
          </div>
          <button class="btn-sm-del" onclick="removeCart(${item.id})">删除</button>
        </div>
      `).join('');
    }

    window.removeCart = function (id) {
      const list = JSON.parse(localStorage.getItem('myCart')) || [];
      const newList = list.filter(v => v.id !== id);
      localStorage.setItem('myCart', JSON.stringify(newList));
      render(); // 删完刷新
    };

    render(); // 首次加载
  </script>
</body>

</html>